package com.propertypoint.presentation.filters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.propertypoint.presentation.model.Advertiesment;
import com.propertypoint.presentation.model.Property;
import com.propertypoint.service.business.AdvertiesmentServiceImpl;
import com.propertypoint.service.business.PropertyFeedServiceImpl;
import com.propertypoint.service.general.AdvertiesmentService;
import com.propertypoint.service.general.PropertyFeedService;
import com.propertypoint.util.PropertyPointConstants;

/**
 * Servlet Filter implementation class PageFilter
 */
@WebFilter("/*")
public class PageFilter implements Filter {
	private PropertyFeedService propertyFeedService = new PropertyFeedServiceImpl();
	private AdvertiesmentService addService=new AdvertiesmentServiceImpl();
    /**
     * Default constructor. 
     */
    public PageFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		HttpServletRequest httpRequest=(HttpServletRequest)request;
		List<Advertiesment> adds=addService.getPublishedAdds();
		HttpSession session=httpRequest.getSession();
		session.setAttribute("addList", adds);
		// pass the request along the filter chain
		if(httpRequest.getRequestURI().endsWith("PropertyPoint/")||httpRequest.getRequestURI().endsWith("PropertyPoint")){
			if(session.getAttribute(PropertyPointConstants.DEFAULT_PROPERTY_LIST)==null){
				List<Property> propertyList=propertyFeedService.searchProperty(request.getParameterMap());
				List<Property> propertyNewList = new ArrayList<Property>();
				int size = 5;
				if(propertyList!=null && propertyList.size() > 5){
					for (int i = 0; i < size; i++) {
						propertyNewList.add(propertyList.get(i));
					}
				}
				session.setAttribute(PropertyPointConstants.DEFAULT_PROPERTY_LIST ,propertyNewList);
			}
			
		}
		if(httpRequest.getRequestURI().endsWith("admin/dashboard.jsp")){
			if(session.getAttribute("admin_login_status")==null||!session.getAttribute("admin_login_status").toString().equals("true")){
				HttpServletResponse httpResponse=(HttpServletResponse)response;
				httpResponse.sendRedirect("../home.jsp");
				return;
			}
		}
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
